package com.gym.code.config.admin.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.gym.code.config.admin.bean.method.MethodColumnMappingBaseBean;
import com.gym.code.config.admin.bean.method.MethodColumnMappingBaseBean.FunctionMappingType;
import com.gym.code.config.admin.dao.IMethodColumnMappingDao;

/**
 * 
 * @author xinchun.wang 
   @email: 532002108@qq.com
 * @createTime 2015-4-11 下午5:47:05
 */
@Repository
public class MethodColumnMappingDaoImpl implements IMethodColumnMappingDao {

	private static final String insertSql = "insert into function_column_mapping_info (function_id, column_id, mapping_type,begin_parameter_name,end_parameter_name,list_flag ) values (?,?,?,?,?,?)";
	private static final String selectSql = "SELECT id, function_id, column_id, mapping_type, begin_parameter_name,end_parameter_name,list_flag FROM function_column_mapping_info ";

	@Autowired
	@Qualifier("jdbcTemplate")
	private JdbcTemplate jdbcTemplate;

	@Override
	public int insert(MethodColumnMappingBaseBean bean) {
		Object[] args = new Object[] {bean.getMethodId(),bean.getColumnId(),bean.getMappingType().getId(),bean.getBeginParameterName(),bean.getEndParameterName(),bean.getListFlag()};
		return jdbcTemplate.update(insertSql, args);
	}

	@Override
	public MethodColumnMappingBaseBean queryBase(long id) {
		List<MethodColumnMappingBaseBean> result = jdbcTemplate.query(selectSql + " where id = ?", new RowMapper<MethodColumnMappingBaseBean>() {
			@Override
			public MethodColumnMappingBaseBean mapRow(ResultSet rs, int rowNum)
					throws SQLException {
				MethodColumnMappingBaseBean bean = new MethodColumnMappingBaseBean();
				bean.setId(rs.getLong("id"));
				bean.setMethodId(rs.getLong("function_id"));
				bean.setColumnId(rs.getLong("column_id"));
				bean.setMappingType(FunctionMappingType.getById(rs.getInt("mapping_type")));
				bean.setBeginParameterName(rs.getString("begin_parameter_name"));
				bean.setEndParameterName(rs.getString("end_parameter_name"));
				bean.setListFlag(rs.getInt("list_flag"));
				return bean;
			
			}
		},id);
		if(CollectionUtils.isEmpty(result)){
			return null;
		}
		return result.get(0);
	}


	@Override
	public List<MethodColumnMappingBaseBean> queryBaseByMethodId(long methodId) {
		List<MethodColumnMappingBaseBean> result = jdbcTemplate.query(selectSql + " where function_id = ?", new RowMapper<MethodColumnMappingBaseBean>() {
			@Override
			public MethodColumnMappingBaseBean mapRow(ResultSet rs, int rowNum)
					throws SQLException {
				MethodColumnMappingBaseBean bean = new MethodColumnMappingBaseBean();
				bean.setId(rs.getLong("id"));
				bean.setMethodId(rs.getLong("function_id"));
				bean.setColumnId(rs.getLong("column_id"));
				bean.setMappingType(FunctionMappingType.getById(rs.getInt("mapping_type")));
				bean.setBeginParameterName(rs.getString("begin_parameter_name"));
				bean.setEndParameterName(rs.getString("end_parameter_name"));
				bean.setListFlag(rs.getInt("list_flag"));
				return bean;
			
			}
		},methodId);
		if(CollectionUtils.isEmpty(result)){
			return Collections.emptyList();
		}
		return result;
	}

}
